package com.ebelter.temperaturegun.ui.view;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.models.bluetooth.products.temperature2.Temperature2Manager;
import com.ebelter.btcomlib.models.https.responses.GetTempertureData;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.TgManager;
import com.ebelter.temperaturegun.model.app.TemUtils;
import com.ebelter.temperaturegun.utils.TemGunUtil;

import java.util.List;

import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_BUTTOM;
import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_CENTER;

public class ListViewDialog extends Dialog implements View.OnClickListener {


    private Context mContext;
    private int mGravity = 2;

    private ListView listviewdialog_lv;
    private LinearLayout date_root_ll;
    private TextView tv_toptitle;
    private TextView tv_cancel;

    private DialogListViewAdapter mDialogListViewAdapter;





    public ListViewDialog(@NonNull Context context) {
        super(context,  R.style.dialog_style);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(false);
        this.mContext = context;
        mGravity=Gravity_CENTER;
    }


    int unitModel;//0代表°C 1代表°F

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final View view = View.inflate(mContext, R.layout.listview_dialog, null);

        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                Window dialogWindow = getWindow();
                if (dialogWindow != null) {

                    dialogWindow.setGravity(Gravity.BOTTOM);
                    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                    DisplayMetrics d = mContext.getResources().getDisplayMetrics();
                    // int maxOne = Math.max(d.widthPixels, d.heightPixels);
                    int minOne = Math.min(d.widthPixels, d.heightPixels);
                    lp.width = (int) (minOne);
//                    lp.height = (int) (minOne * 0.3);
                    // lp.width= DipUtil.dip2px(getContext(),288);
                    lp.height= LinearLayout.LayoutParams.WRAP_CONTENT;
                    dialogWindow.setAttributes(lp);
                }
            }
        });
        setContentView(view);
        getWindow().setGravity(Gravity.BOTTOM);
        FV();
        setListeners();
        initDatas();

    }


    public void setTopTitle(String topTitle){
        ViewUtils.setTextViewStr(tv_toptitle,topTitle);
    }

    List<GetTempertureData.ResultDataBean.DataBean> datas;
    public void setDatas(List<GetTempertureData.ResultDataBean.DataBean> data) {
        this.datas=data;
        if (mDialogListViewAdapter!=null){
            mDialogListViewAdapter.notifyDataSetChanged();
        }
    }
    StringBuilder sb = new StringBuilder();
    class DialogListViewAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return datas==null?0:datas.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            VH vh;
            if (convertView==null){
                convertView = View.inflate(getContext(),R.layout.listviewdialog_item,null);
                vh=new VH();
                vh.value = convertView.findViewById(R.id.listdialog_value_tv);
                vh.test_date = convertView.findViewById(R.id.listdialog_num_tv);
                vh.desc = convertView.findViewById(R.id.listdialog_desc_tv);
                convertView.setTag(vh);
            }else {
                vh = (VH) convertView.getTag();
            }

            GetTempertureData.ResultDataBean.DataBean dataBean = datas.get(position);
            if (unitModel==0){//0代表°C 1代表°F

                ViewUtils.setTextViewStr(vh.value, TemGunUtil.getDisTemC(dataBean.getTemperature())+" °C");
            }else {
                ViewUtils.setTextViewStr(vh.value, TemGunUtil.getDisTemF(dataBean.getTemperature())+" °F");
            }

            long time = TimeUtils.parseFormatter1Time(dataBean.testDate).getTime();
            sb.setLength(0);
            //pager_goal_suggtion2_tv.setText(String.format(StringUtils.getResStr(R.string.Gain_1_1f_to_achieve_this_goal), differenceValue, unit));
            //sb.append("第").append(position+1).append("次测量 ").append(TimeUtils.formatTime_shf(getContext(),time));
            sb.append(String.format(CommonLib.getString(R.string.di_n_ciceliang),(getCount()-position)+"")).append(TimeUtils.formatTime_shf(getContext(),time));
            ViewUtils.setTextViewStr(vh.test_date, sb.toString());

            ViewUtils.setTextViewStr(vh.desc, TemUtils.getResultDesc(dataBean.getTemperature()));
            return convertView;
        }
    }

    class VH{
        TextView value;
        TextView test_date;
        TextView desc;
    }





    public void setGravity(int gravity){
        this.mGravity=gravity;
    }

    private void initDatas() {
        unitModel = TemUtils.getUerUnit();
        mDialogListViewAdapter=new DialogListViewAdapter();
        listviewdialog_lv.setAdapter(mDialogListViewAdapter);
        listviewdialog_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (datas!=null&&position>=0&&position<datas.size()){
                    if (listener!=null){listener.selectItem(ListViewDialog.this,datas.get(position));}
                }
            }
        });

    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        void cancel(Dialog dialogView);
        void selectItem(Dialog dialogView,GetTempertureData.ResultDataBean.DataBean dataBean);
    }

    private void setListeners() {
        tv_toptitle.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    private void FV() {
        date_root_ll = findViewById(R.id.date_root_ll);
        tv_toptitle = findViewById(R.id.tv_toptitle);
        tv_cancel = findViewById(R.id.tv_cancel);
        listviewdialog_lv = findViewById(R.id.listviewdialog_lv);
    }



    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_cancel) {
            if (listener != null) {
                listener.cancel(this);
            }
        }

    }





}
